package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import controller.Controller;

import model.Composite;

/**
 * Composite elem megjelenítéséért felelős osztály
 * JPanel és MouseListener leszármazás és implementálás a kattinthatóságért
 * @author radioactivebee
 *
 */
public class CompositeView extends JPanel implements Paintable, MouseListener, ModelProvider {

	private static final long serialVersionUID = 1L;
	private int x;
	private int y;
	private Composite comp;
	public List<InputView> inputViews = new ArrayList<InputView>();
	public List<OutputView> outputViews = new ArrayList<OutputView>();
	
	public CompositeView(int x, int y, Composite comp) {
		super();
		this.x = x;
		this.y = y;
		this.comp = comp;
		Integer inNum = comp.getInputs().size();
		Integer outNum = comp.getOutputs().size();
		Integer stepIn = (inNum == 0) ? 40 : 70/inNum;
		Integer stepOut = (inNum == 0) ? 40 : 70/outNum;
		for (int i = 0; i<inNum; i++){
			inputViews.add(new InputView(x-5, y+15+i*stepIn, comp.getInputs().get(i), comp));
		}
		for (int i = 0; i<outNum; i++){
			outputViews.add(new OutputView(x+105, y+15+i*stepOut, comp.getOutputs().get(i), comp));
		}
		setBounds(this.x, this.y, 100, 100);
		addMouseListener(this);
	}
	
	@Override
	public void mouseClicked(MouseEvent e){
		if(Controller.verbose){
			System.out.println("compositeview mouse listener invoked");		
		}
		Controller.getInstance().compositeClicked(this);
		Controller.getInstance().blockClicked(this);
	}
		
	@Override
	public void paint(Graphics g) {
		Image img = new ImageIcon("img/composite.png").getImage();
		g.setColor(Color.BLACK);
		g.drawString(comp.getName(), x, y-5);

		g.drawImage(img, x, y, null);
		for (Paintable p :  inputViews){
			p.paint(g);
		}
		for (Paintable p :  outputViews){
			p.paint(g);
		}
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Composite getModel(){
		return comp;
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
	}

	@Override
	public void mousePressed(MouseEvent arg0) {	
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
	}
}
